package com.wcsy.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcsy.myblog.entity.Announcement;
import com.wcsy.myblog.entity.JsonResult;
import com.wcsy.myblog.service.IAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告
 */
@CrossOrigin
@RestController
@RequestMapping("announcement")
@Api(description = "公告")
@ApiModel
public class AnnouncementController {
    @Autowired
    IAnnouncementService iAnnouncementService;
    /**
     * 添加公告
     * @param announcementContent  公告内容
     */
    @ApiOperation("添加公告")
    @GetMapping("insert")
    public JsonResult insertAnnouncement(String announcementContent){
        try {
            iAnnouncementService.insertAnnouncement(announcementContent);
            return new JsonResult(200,"添加成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"添加失败","");
        }
    }
    /**
     * 显示最近发布的一篇公告及所有公告
     */
    @ApiOperation("显示最近发布的一篇公告")
    @GetMapping("selectOneAnnouncement")
    public JsonResult selectOneAnnouncement(){
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            //所有的公告
            List<Announcement> list = iAnnouncementService.list();
            map.put("announcementList",list);
            //最近发布的公告
            if (!list.isEmpty()){
                Announcement announcement = list.get(list.size() - 1);
                map.put("oneAnnouncement",announcement);
            }
            return new JsonResult(200,"success",map);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(500,"failure",null);
        }
    }
    /**
     * 修改公告
     * @param announcementContent 修改的公告内容
     * @param announcementId 公告id
     */
    @ApiOperation("修改公告")
    @GetMapping("update")
    public JsonResult update(int announcementId,String announcementContent){
        try {
            iAnnouncementService.updateAnnouncement(announcementContent,announcementId);
            return new JsonResult(200,"success",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"failure",null);
        }
    }
    /**
     * 删除公告
     */
    @ApiOperation("删除公告")
    @GetMapping("delete")
    public JsonResult remove(int announcementId){
        boolean announcement_id = iAnnouncementService.
                remove(new QueryWrapper<Announcement>().eq("announcement_id", announcementId));
        if (announcement_id){
            return new JsonResult(200,"success",null);
        }else {
            return new JsonResult(500,"failure",null);
        }
    }
}
